unit pcneGerar;

interface uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, ComCtrls, ExtCtrls, Grids, StdCtrls;

function GerarXMLNFe: Boolean;

implementation uses

  principal, pcnNFe, pcnConversao, pcneAuxiliar;

function GerarXMLNFe: Boolean;
var
  i, j: integer;
  iRow1, iRow2, iCol, icol2: integer;
  iItem, iDI, iMed, iArma, iForDia, iReduc: integer;
  NFe: TNFe;
  ok: boolean;
  s: string;
begin

  Result := true;

  NFe := TNFe.create;
  pgrid := TstringGrid.Create(nil);

  pgrid := ForPrincipal.G10documento;

  NFe.Ide.cUF := rI('B02');
  NFe.Ide.cNF := rI('B03');
  NFe.Ide.natOp := rC('B04');
  NFe.Ide.indPag := StrToIndPag(ok, rT('B05'));
  NFe.Ide.modelo := rI('B06');
  NFe.Ide.serie := rI('B07');
  NFe.Ide.nNF := rI('B08');
  NFe.Ide.dEmi := rD('B09');
  NFe.Ide.dSaiEnt := rD('B10');
  NFe.Ide.tpNF := StrTotpNF(ok, rT('B11'));
  NFe.Ide.cMunFG := rI('B12');
  NFe.Ide.tpImp := StrTotpImp(ok, rT('B21'));
  NFe.Ide.tpEmis := StrTotpEmis(ok, rT('B22'));
  NFe.Ide.cDv := rI('B23');
  NFe.Ide.tpAmb := StrTotpAmb(ok, rT('B24'));
  NFe.Ide.finNFe := StrTofinNFe(ok, rT('B25'));
  NFe.Ide.procEmi := StrToprocEmi(ok, rT('B26'));
  NFe.Ide.verProc := rC('B27');
  NFe.Ide.dhCont := rDH('B28');
  NFe.Ide.xJust := rC('B29');

  pgrid := ForPrincipal.G20refNFe;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    prow := i;
    if TU(rC('B13')) <> '' then
    begin
      NFe.Ide.NFref.Add;
      j := NFe.Ide.NFref.Count - 1;
      NFe.Ide.NFref[j].refNFe := rC('B13');
    end;
  end;

  pgrid := ForPrincipal.G20refNF;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    prow := i;
    if rI('B15') <> 0 then
    begin
      NFe.Ide.NFref.Add;
      j := NFe.Ide.NFref.Count - 1;
      NFe.Ide.NFref[j].RefNF.cUF := rI('B15');
      NFe.Ide.NFref[j].RefNF.AAMM := rC('B16');
      NFe.Ide.NFref[j].RefNF.CNPJ := rC('B17');
      NFe.Ide.NFref[j].RefNF.modelo := rI('B18');
      NFe.Ide.NFref[j].RefNF.serie := rI('B19');
      NFe.Ide.NFref[j].RefNF.nNF := rI('B20');
    end;
  end;

  pgrid := ForPrincipal.G20refNFP;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    prow := i;
    if rI('B20b') <> 0 then
    begin
      NFe.Ide.NFref.Add;
      j := NFe.Ide.NFref.Count - 1;
      NFe.Ide.NFref[j].RefNFP.cUF := rI('B20b');
      NFe.Ide.NFref[j].RefNFP.AAMM := rC('B20c');
      NFe.Ide.NFref[j].RefNFP.CNPJCPF := Trim(rC('B20d'));
      NFe.Ide.NFref[j].RefNFP.Modelo := rI('B20f');
      NFe.Ide.NFref[j].RefNFP.Serie := rI('B20g');
      NFe.Ide.NFref[j].RefNFP.nNF := rI('B20h');
    end;
  end;

  pgrid := ForPrincipal.G20refCTe;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    prow := i;
    if TU(rC('B20i')) <> '' then
    begin
      NFe.Ide.NFref.Add;
      j := NFe.Ide.NFref.Count - 1;
      NFe.Ide.NFref[j].refCTe := rC('B20i');
    end;
  end;

  pgrid := ForPrincipal.G20refECF;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    prow := i;
    if rI('B20l') <> 0 then
    begin
      NFe.Ide.NFref.Add;
      j := NFe.Ide.NFref.Count - 1;
      NFe.Ide.NFref[j].RefECF.modelo := StrToModeloECF(ok, rT('B20k'));
      NFe.Ide.NFref[j].RefECF.nECF := rI('B20l');
      NFe.Ide.NFref[j].RefECF.nCOO := rI('B20m');
    end;
  end;

  pgrid := ForPrincipal.G10emissor;
  NFe.Emit.CNPJCPF := rC('C02');
  NFe.Emit.xNome := rC('C03');
  NFe.Emit.xFant := rC('C04');
  NFe.Emit.enderEmit.xLgr := rC('C06');
  NFe.Emit.enderEmit.nro := rC('C07');
  NFe.Emit.enderEmit.xCpl := rC('C08');
  NFe.Emit.enderEmit.xBairro := rC('C09');
  NFe.Emit.enderEmit.cMun := rI('C10');
  NFe.Emit.enderEmit.xMun := rC('C11');
  NFe.Emit.enderEmit.UF := rC('C12');
  NFe.Emit.enderEmit.CEP := rI('C13');
  NFe.Emit.enderEmit.cPais := rI('C14');
  NFe.Emit.enderEmit.xPais := rC('C15');
  NFe.Emit.enderEmit.fone := rC('C16');
  NFe.Emit.IE := rC('C17');
  NFe.Emit.IEST := rC('C18');
  NFe.Emit.IM := rC('C19');
  NFe.Emit.CNAE := rC('C20');
  NFe.Emit.CRT := StrToCRT(ok, rT('C21'));

  pgrid := ForPrincipal.G10destinatario;
  NFe.Dest.CNPJCPF := rC('E02');
  NFe.Dest.xNome := rC('E04');
  NFe.Dest.IE := rC('E17');
  NFe.Dest.ISUF := rC('E18');
  NFe.Dest.enderDest.xLgr := rC('E06');
  NFe.Dest.enderDest.nro := rC('E07');
  NFe.Dest.enderDest.xCpl := rC('E08');
  NFe.Dest.enderDest.xBairro := rC('E09');
  NFe.Dest.enderDest.cMun := rI('E10');
  NFe.Dest.enderDest.xMun := rC('E11');
  NFe.Dest.enderDest.UF := rC('E12');
  NFe.Dest.enderDest.CEP := rI('E13');
  NFe.Dest.enderDest.cPais := rI('E14');
  NFe.Dest.enderDest.xPais := rC('E15');
  NFe.Dest.enderDest.fone := rC('E16');

  pgrid := ForPrincipal.G10retirada;
  NFe.Retirada.CNPJCPF := rC('F02');
  NFe.Retirada.xLgr := rC('F03');
  NFe.Retirada.nro := rC('F04');
  NFe.Retirada.xCpl := rC('F05');
  NFe.Retirada.xBairro := rC('F06');
  NFe.Retirada.cMun := rI('F07');
  NFe.Retirada.xMun := rC('F08');
  NFe.Retirada.UF := rC('F09');

  pgrid := ForPrincipal.G10entrega;
  NFe.Entrega.CNPJCPF := rC('G02');
  NFe.Entrega.xLgr := rC('G03');
  NFe.Entrega.nro := rC('G04');
  NFe.Entrega.xCpl := rC('G05');
  NFe.Entrega.xBairro := rC('G06');
  NFe.Entrega.cMun := rI('G07');
  NFe.Entrega.xMun := rC('G08');
  NFe.Entrega.UF := rC('G09');

  //
  iItem := -1;
  iCol := ColGrid('K=1', ForPrincipal.G20itens);
  for iRow1 := ForPrincipal.G20itens.FixedRows - 1 to ForPrincipal.G20itens.RowCount - 1 do
  begin
    ForPrincipal.G20itens.row := iRow1;
    if (trim(ForPrincipal.G20itens.Cells[iCol, iRow1]) <> '') then
    begin
      if (trim(ForPrincipal.G20itens.Cells[ColGrid('I02', ForPrincipal.G20itens), iRow1]) <> '') then
      begin

        pgrid := ForPrincipal.g11produtos;
        NFe.Det.Add;
        inc(iItem);

        NFe.Det[iItem].prod.nItem := iItem + 1;
        //    (*V01*)NFe.Det[i].infAdProd := Leitor.rCampo(tcStr, 'infAdProd');

        NFe.Det[iItem].Prod.cProd := rC('I02');
        NFe.Det[iItem].Prod.cEAN := rC('I03');
        NFe.Det[iItem].Prod.xProd := rC('I04');
        NFe.Det[iItem].Prod.NCM := rC('I05');
        NFe.Det[iItem].Prod.EXTIPI := rC('I06');
        NFe.Det[iItem].Prod.genero := rI('I07');
        NFe.Det[iItem].Prod.CFOP := rC('I08');
        NFe.Det[iItem].Prod.uCom := rC('I09');
        NFe.Det[iItem].Prod.qCom := rN('I10');
        NFe.Det[iItem].Prod.vUnCom := rN('I10a');
        NFe.Det[iItem].Prod.vProd := rN('I11');
        NFe.Det[iItem].Prod.cEANTrib := rC('I12');
        NFe.Det[iItem].Prod.uTrib := rC('I13');
        NFe.Det[iItem].Prod.qTrib := rN('I14');
        NFe.Det[iItem].Prod.vUnTrib := rN('I14a');
        NFe.Det[iItem].Prod.vFrete := rN('I15');
        NFe.Det[iItem].Prod.vSeg := rN('I16');
        NFe.Det[iItem].Prod.vDesc := rN('I17');
        NFe.Det[iItem].Prod.vOutro := rN('I17a');
        NFe.Det[iItem].Prod.IndTot := StrToindTot(ok, extrairIdentificador(rC('I17b')));

        pgrid := ForPrincipal.G11Veiculos;

        NFe.Det[iItem].Prod.veicProd.tpOP := StrToTpOP(ok, extrairIdentificador(rC('J02')));
        NFe.Det[iItem].Prod.veicProd.chassi := rC('J03');
        NFe.Det[iItem].Prod.veicProd.cCor := rC('J04');
        NFe.Det[iItem].Prod.veicProd.xCor := rC('J05');
        NFe.Det[iItem].Prod.veicProd.pot := rC('J06');
        NFe.Det[iItem].Prod.veicProd.CM3 := rC('J07');
        NFe.Det[iItem].Prod.veicProd.Cilin := rC('J07');
        NFe.Det[iItem].Prod.veicProd.pesoL := rC('J08');
        NFe.Det[iItem].Prod.veicProd.pesoB := rC('J09');
        NFe.Det[iItem].Prod.veicProd.nSerie := rC('J10');
        NFe.Det[iItem].Prod.veicProd.tpComb := rC('J11');
        NFe.Det[iItem].Prod.veicProd.nMotor := rC('J12');
        NFe.Det[iItem].Prod.veicProd.CMKG := rC('J13');
        NFe.Det[iItem].Prod.veicProd.CMT := rC('J13');
        NFe.Det[iItem].Prod.veicProd.dist := rC('J14');
        NFe.Det[iItem].Prod.veicProd.RENAVAM := rC('J15');
        NFe.Det[iItem].Prod.veicProd.anoMod := rI('J16');
        NFe.Det[iItem].Prod.veicProd.anoFab := rI('J17');
        NFe.Det[iItem].Prod.veicProd.tpPint := rC('J18');
        NFe.Det[iItem].Prod.veicProd.tpVeic := rI('J19');
        NFe.Det[iItem].Prod.veicProd.espVeic := rI('J20');
        NFe.Det[iItem].Prod.veicProd.VIN := rC('J21');
        NFe.Det[iItem].Prod.veicProd.condVeic := StrToCondVeic(ok, extrairIdentificador(rC('J22')));
        NFe.Det[iItem].Prod.veicProd.cMod := rC('J23');
        //ver        NFe.Det[iItem].Prod.veicProd.cCorDEN := rC('J24');
        NFe.Det[iItem].Prod.veicProd.lota := rI('J25');
        //ver        NFe.Det[iItem].Prod.veicProd.tpRest := rI('J26');

        pgrid := ForPrincipal.G21DI;
        iCol2 := ColGrid('K=2', pgrid);
        for iRow2 := pgrid.FixedRows - 1 to pgrid.RowCount - 1 do
        begin
          pgrid.row := iRow2;
          if (trim(pgrid.Cells[iCol2, iRow2]) <> '') then
          begin
            NFe.Det[iItem].Prod.DI.Add;
            iDI := NFe.Det[iItem].Prod.DI.Count - 1;
            NFe.Det[iItem].Prod.DI[iDI].nDI := rC('I19');
            NFe.Det[iItem].Prod.DI[iDI].dDI := rD('I20');
            NFe.Det[iItem].Prod.DI[iDI].xLocDesemb := rC('I21');
            NFe.Det[iItem].Prod.DI[iDI].UFDesemb := rC('I22');
            NFe.Det[iItem].Prod.DI[iDI].dDesemb := rD('I23');
            NFe.Det[iItem].Prod.DI[iDI].cExportador := rC('I24');
          end;
        end;

        pgrid := ForPrincipal.G21medicamentos;
        iCol2 := ColGrid('K02', pgrid);
        for iRow2 := pgrid.FixedRows - 1 to pgrid.RowCount - 1 do
        begin
          pgrid.row := iRow2;
          if (trim(pgrid.Cells[iCol2, iRow2]) <> '') then
          begin
            NFe.Det[iItem].Prod.med.Add;
            iMed := NFe.Det[iItem].Prod.med.Count - 1;
            NFe.Det[iItem].Prod.med[iMed].nLote := rC('K02');
            NFe.Det[iItem].Prod.med[iMed].qLote := rN('K03');
            NFe.Det[iItem].Prod.med[iMed].dFab := rD('K041');
            NFe.Det[iItem].Prod.med[iMed].dVal := rD('K05');
            NFe.Det[iItem].Prod.med[iMed].vPMC := rN('K06');
          end;
        end;

        pgrid := ForPrincipal.G21armamentos;
        iCol2 := ColGrid('L03', pgrid);
        for iRow2 := pgrid.FixedRows - 1 to pgrid.RowCount - 1 do
        begin
          pgrid.row := iRow2;
          if (trim(pgrid.Cells[iCol2, iRow2]) <> '') then
          begin
            NFe.Det[iItem].Prod.arma.Add;
            iArma := NFe.Det[iItem].Prod.arma.Count - 1;
            NFe.Det[iItem].Prod.arma[iArma].tpArma := StrToTpArma(ok, extrairIdentificador(rC('L02')));
            NFe.Det[iItem].Prod.arma[iArma].nSerie := rI('L03');
            NFe.Det[iItem].Prod.arma[iArma].nCano := rI('L04');
            NFe.Det[iItem].Prod.arma[iArma].descr := rC('L05');
          end;
        end;

        pGrid := ForPrincipal.G21Combustiveis;
        iCol2 := ColGrid('K=3', pGrid);
        for iRow2 := pGrid.FixedRows - 1 to pGrid.RowCount - 1 do
        begin
          pGrid.row := iRow2;
          if (trim(pGrid.Cells[iCol2, iRow2]) <> '') then
          begin
            //NFe.Det[iItem].Prod.comb.
            //inc(iMed);
            //NFe.Det[iItem].Prod.comb.
            //NFe.Det[iItem].Prod.comb.
            //NFe.Det[iItem].Prod.comb.
            //NFe.Det[iItem].Prod.comb.
            //NFe.Det[iItem].Prod.comb.
          end;
        end;

        pgrid := ForPrincipal.G11ICMS;

        NFe.Det[iItem].Imposto.ICMS.orig := StrToOrig(ok, extrairIdentificador(rC('N11')));
        NFe.Det[iItem].Imposto.ICMS.CST := StrToCSTICMS(ok, extrairIdentificador(rC('N12')));
        NFe.Det[iItem].Imposto.ICMS.CSOSN := StrToCSOSN(ok, extrairIdentificador(rC('N12a')));
        NFe.Det[iItem].Imposto.ICMS.modBC := StrTomodBC(ok, extrairIdentificador(rC('N13')));
        NFe.Det[iItem].Imposto.ICMS.pRedBC := rN('N14');
        NFe.Det[iItem].Imposto.ICMS.vBC := rN('N15');
        NFe.Det[iItem].Imposto.ICMS.pICMS := rN('N16');
        NFe.Det[iItem].Imposto.ICMS.vICMS := rN('N17');
        NFe.Det[iItem].Imposto.ICMS.modBCST := StrTomodBCST(ok, extrairIdentificador(rC('N18')));
        NFe.Det[iItem].Imposto.ICMS.pMVAST := rN('N19');
        NFe.Det[iItem].Imposto.ICMS.pRedBCST := rN('N20');
        NFe.Det[iItem].Imposto.ICMS.vBCST := rN('N21');
        NFe.Det[iItem].Imposto.ICMS.pICMSST := rN('N22');
        NFe.Det[iItem].Imposto.ICMS.vICMSST := rN('N23');
        NFe.Det[iItem].Imposto.ICMS.UFST := rC('N24');
        NFe.Det[iItem].Imposto.ICMS.pBCOp := rN('N25');
        NFe.Det[iItem].Imposto.ICMS.vBCSTRet := rN('N26');
        NFe.Det[iItem].Imposto.ICMS.vICMSSTRet := rN('N27');
        NFe.Det[iItem].Imposto.ICMS.motDesICMS := StrTomotDesICMS(ok, extrairIdentificador(rC('N28')));
        NFe.Det[iItem].Imposto.ICMS.pCredSN := rN('N29');
        NFe.Det[iItem].Imposto.ICMS.vCredICMSSN := rN('N30');
        NFe.Det[iItem].Imposto.ICMS.vBCSTDest := rN('N31');
        NFe.Det[iItem].Imposto.ICMS.vICMSSTDest := rN('N32');

        pgrid := ForPrincipal.G11IPI;

        NFe.Det[iItem].Imposto.IPI.clEnq := rC('O02');
        NFe.Det[iItem].Imposto.IPI.CNPJProd := rC('O03');
        NFe.Det[iItem].Imposto.IPI.cSelo := rC('O04');
        NFe.Det[iItem].Imposto.IPI.qSelo := rI('O05');
        NFe.Det[iItem].Imposto.IPI.cEnq := rC('O06');
        NFe.Det[iItem].Imposto.IPI.CST := StrToCSTIPI(ok, extrairIdentificador(rC('O09')));
        NFe.Det[iItem].Imposto.IPI.vBC := rN('O10');
        NFe.Det[iItem].Imposto.IPI.qUnid := rN('O11');
        NFe.Det[iItem].Imposto.IPI.vUnid := rN('O12');
        NFe.Det[iItem].Imposto.IPI.pIPI := rN('O13');
        NFe.Det[iItem].Imposto.IPI.vIPI := rN('O14');

        pgrid := ForPrincipal.G11II;

        NFe.Det[iItem].Imposto.II.vBc := rN('P02');
        NFe.Det[iItem].Imposto.II.vDespAdu := rN('P03');
        NFe.Det[iItem].Imposto.II.vII := rN('P04');
        NFe.Det[iItem].Imposto.II.vIOF := rN('P05');

        pgrid := ForPrincipal.G11PIS;

        NFe.Det[iItem].Imposto.PIS.CST := StrToCSTPIS(ok, extrairIdentificador(rC('Q06')));
        NFe.Det[iItem].Imposto.PIS.vBC := rN('Q07');
        NFe.Det[iItem].Imposto.PIS.pPIS := rN('Q08');
        NFe.Det[iItem].Imposto.PIS.vPIS := rN('Q09');
        NFe.Det[iItem].Imposto.PIS.qBCProd := rN('Q10');
        NFe.Det[iItem].Imposto.PIS.vAliqProd := rN('Q11');

        pgrid := ForPrincipal.G11PISST;

        NFe.Det[iItem].Imposto.PISST.vBc := rN('R02');
        NFe.Det[iItem].Imposto.PISST.pPis := rN('R03');
        NFe.Det[iItem].Imposto.PISST.qBCProd := rN('R04');
        NFe.Det[iItem].Imposto.PISST.vAliqProd := rN('R05');
        NFe.Det[iItem].Imposto.PISST.vPIS := rN('R06');

        pgrid := ForPrincipal.G11COFINS;

        NFe.Det[iItem].Imposto.COFINS.CST := StrToCSTCOFINS(ok, extrairIdentificador(rC('S06')));
        NFe.Det[iItem].Imposto.COFINS.vBC := rN('S07');
        NFe.Det[iItem].Imposto.COFINS.pCOFINS := rN('S08');
        NFe.Det[iItem].Imposto.COFINS.qBCProd := rN('S09');
        NFe.Det[iItem].Imposto.COFINS.vAliqProd := rN('S10');
        NFe.Det[iItem].Imposto.COFINS.vCOFINS := rN('S11');

        pgrid := ForPrincipal.G11COFINSST;

        NFe.Det[iItem].Imposto.COFINSST.vBC := rN('T02');
        NFe.Det[iItem].Imposto.COFINSST.pCOFINS := rN('T03');
        NFe.Det[iItem].Imposto.COFINSST.qBCProd := rN('T04');
        NFe.Det[iItem].Imposto.COFINSST.vAliqProd := rN('T05');
        NFe.Det[iItem].Imposto.COFINSST.vCOFINS := rN('T06');

        pgrid := ForPrincipal.G11ISSQN;

        NFe.Det[iItem].Imposto.ISSQN.vBC := rN('U02');
        NFe.Det[iItem].Imposto.ISSQN.vAliq := rN('U03');
        NFe.Det[iItem].Imposto.ISSQN.vISSQN := rN('U04');
        NFe.Det[iItem].Imposto.ISSQN.cMunFG := rI('U05');
        NFe.Det[iItem].Imposto.ISSQN.cListServ := rI('U06');
        NFe.Det[iItem].Imposto.ISSQN.cSitTrib := StrToCSitTrib(ok, extrairIdentificador(rC('U07')));

      end;

    end;

  end;

  //

  pgrid := ForPrincipal.G10TotaisICMS;
  NFe.Total.ICMSTot.vBC := rN('W03');
  NFe.Total.ICMSTot.vICMS := rN('W04');
  NFe.Total.ICMSTot.vBCST := rN('W05');
  NFe.Total.ICMSTot.vST := rN('W06');
  NFe.Total.ICMSTot.vProd := rN('W07');
  NFe.Total.ICMSTot.vFrete := rN('W08');
  NFe.Total.ICMSTot.vSeg := rN('W09');
  NFe.Total.ICMSTot.vDesc := rN('W10');
  NFe.Total.ICMSTot.vII := rN('W11');
  NFe.Total.ICMSTot.vIPI := rN('W12');
  NFe.Total.ICMSTot.vPIS := rN('W13');
  NFe.Total.ICMSTot.vCOFINS := rN('W14');
  NFe.Total.ICMSTot.vOutro := rN('W15');
  NFe.Total.ICMSTot.vNF := rN('W16');

  pgrid := ForPrincipal.G10TotaisISSQN;
  NFe.Total.ISSQNtot.vServ := rN('W18');
  NFe.Total.ISSQNtot.vBC := rN('W19');
  NFe.Total.ISSQNtot.vISS := rN('W20');
  NFe.Total.ISSQNtot.vPIS := rN('W21');
  NFe.Total.ISSQNtot.vCOFINS := rN('W22');

  pgrid := ForPrincipal.G10TotaisRetido;
  NFe.Total.retTrib.vRetPIS := rN('W24');
  NFe.Total.retTrib.vRetCOFINS := rN('W25');
  NFe.Total.retTrib.vRetCSLL := rN('W26');
  NFe.Total.retTrib.vBCIRRF := rN('W27');
  NFe.Total.retTrib.vIRRF := rN('W28');
  NFe.Total.retTrib.vBCRetPrev := rN('W29');
  NFe.Total.retTrib.vRetPrev := rN('W30');

  pgrid := ForPrincipal.G10Transporte;
  NFe.Transp.modFrete := StrToModFrete(ok, rT('X02'));
  NFe.Transp.vagao := rC('X25a');
  NFe.Transp.balsa := rC('X25b');
  NFe.Transp.Transporta.CNPJCPF := rC('X05');
  NFe.Transp.Transporta.xNome := rC('X06');
  NFe.Transp.Transporta.IE := rC('X07');
  NFe.Transp.Transporta.xEnder := rC('X08');
  NFe.Transp.Transporta.xMun := rC('X09');
  NFe.Transp.Transporta.UF := rC('X10');
  NFe.Transp.retTransp.vServ := rN('X12');
  NFe.Transp.retTransp.vBCRet := rN('X13');
  NFe.Transp.retTransp.pICMSRet := rN('X14');
  NFe.Transp.retTransp.vICMSRet := rN('X15');
  NFe.Transp.retTransp.CFOP := rC('X16');
  NFe.Transp.retTransp.cMunFG := rI('X17');
  NFe.Transp.veicTransp.placa := rC('X19');
  NFe.Transp.veicTransp.UF := rC('X20');
  NFe.Transp.veicTransp.RNTC := rC('X21');

  pgrid := ForPrincipal.G10fatura;
  NFe.Cobr.Fat.nFat := rC('Y03');
  NFe.Cobr.Fat.vOrig := rN('Y04');
  NFe.Cobr.Fat.vDesc := rN('Y05');
  NFe.Cobr.Fat.vLiq := rN('Y06');

  pgrid := ForPrincipal.G20duplicatas;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    if rI('B20l') <> 0 then
    begin
      NFe.Cobr.Dup.Add;
      j := NFe.Cobr.Dup.Count - 1;
      NFe.Cobr.Dup[j].nDup := rC('Y08');
      NFe.Cobr.Dup[j].dVenc := rD('Y09');
      NFe.Cobr.Dup[j].vDup := rN('Y10');
    end;
  end;

  pGrid := ForPrincipal.G20AdicionaisFisco;
  iCol := ColGrid('Z02', pGrid);
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    s := Trim(pGrid.Cells[iCol, i]);
    if s <> '' then
      s := s + ';';
    NFe.InfAdic.infAdFisco := s; (*Z02*)
  end;

  pGrid := ForPrincipal.G20AdicionaisContribuinte;
  iCol := ColGrid('Z03', pGrid);
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    s := Trim(pGrid.Cells[iCol, i]);
    if s <> '' then
      s := s + ';';
    NFe.InfAdic.infCpl := s; (*Z03*)
  end;

  pGrid := ForPrincipal.G20LivreContribuinte;
  iCol := ColGrid('Z05', pGrid);
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    if Trim(rC('Z05')) <> '' then
    begin
      NFe.InfAdic.obsCont[i].xCampo := rC('Z05');
      NFe.InfAdic.obsCont[i].xTexto := rC('Z06');
    end;
  end;

  pGrid := ForPrincipal.G20LivreFisco;
  iCol := ColGrid('Z08', pGrid);
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    if Trim(rC('Z08')) <> '' then
    begin
      NFe.InfAdic.obsFisco[i].xCampo := rC('Z08');
      NFe.InfAdic.obsFisco[i].xTexto := rC('Z09');
    end;
  end;

  pGrid := ForPrincipal.G20ProcessoReferenciado;
  iCol := ColGrid('Z11', pGrid);
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    if Trim(rC('Z11')) <> '' then
    begin
      NFe.InfAdic.procRef[i].nProc := rC('Z11');
      NFe.InfAdic.procRef[i].indProc := StrToIndProc(ok, extrairIdentificador(rC('Z12')));
    end;
  end;

  pgrid := ForPrincipal.G10Cana;
  NFe.cana.safra := rC('ZC02');
  NFe.cana.ref := rC('ZC03');
  NFe.cana.qTotMes := rN('ZC07');
  NFe.cana.qTotAnt := rN('ZC08');
  NFe.cana.qTotGer := rN('ZC09');
  NFe.cana.vFor := rN('ZC13');
  NFe.cana.vTotDed := rN('ZC14');
  NFe.cana.vLiqFor := rN('ZC15');

  pgrid := ForPrincipal.G10ForDia;
  for iForDia := 0 to 30 do
  begin
    pGrid.Row := pGrid.FixedRows + iForDia;
    if rN('ZC06') > 0 then
    begin
      NFe.cana.fordia.Add;
      NFe.cana.fordia[iForDia].dia := iForDia + 1;
      NFe.cana.fordia[iForDia].qtde := rN('ZC06');
    end;
  end;

  pgrid := ForPrincipal.G20Deduc;
  for i := pgrid.FixedRows to pgrid.RowCount - 1 do
  begin
    pgrid.row := i;
    if rI('ZC11') <> 0 then
    begin
      NFe.Cana.deduc.Add;
      j := NFe.Cana.Deduc.Count - 1;
      NFe.Cana.Deduc[j].xDed := rC('ZC11');
      NFe.Cana.Deduc[j].vDed := rN('ZC12');
    end;
  end;

end;

end.

